# cookie 三天免登录
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
#登录视图
def login(request):
    #判断请求
    if request.method =='GET':

        # if request.COOKIES.has_key('login'):  #has_key()是否有这个key  3版本作废
        # 判断客户端是否有login对应的cookie信息

        if 'login' in request.COOKIES:
            # 'qq','12'
            strs =   request.COOKIES.get('login').split(',')   #split() 通过指定分隔符对字符串进行切片
            uname = strs[0]
            pword = strs[1]
            # 获取下来传到前端
            return render(request,'login.html',{'uname':uname,'pword':pword})
        return render(request,'login.html')
    else:
        #从前端获取数据
        name = request.POST.get('uname')
        pword = request.POST.get('pword')
        l = request.POST.get('yy')
        response = HttpResponse()
        if name=='qq' and pword =='12':
            # 判断记住密码是否打勾
            if l == '1':
                #创建cookie对象
                response.content = '登录成功！'
                #记住密码
                response.set_cookie('login',name+','+pword,path='/login/',max_age=24*60*60*3)
                return response
            else: #如果不是，删除cookie
                response.delete_cookie('login', path='/login/')
                response.content = '登录成功！'
        else:
            response.delete_cookie('login',path='/login/')
            response.content = '登录失败！'
        return response